package com.lin.servlet;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lin.service.BookServiceImpl;
import com.lin.service.CategoryServiceImpl;
import com.lin.service.IBookService;
import com.lin.service.ICategoryService;
import com.lin.utils.BaseServlet;
import com.lin.vo.Book;
import com.lin.vo.Category;
@WebServlet(name="AdminBookServlet",urlPatterns={"/adminBookServlet"})
public class AdminBookServlet extends BaseServlet{
	
	private static final long serialVersionUID = 1L;
	private IBookService bookService=new  BookServiceImpl();
	private ICategoryService categoryService =new CategoryServiceImpl();
	public String findAllBook(HttpServletRequest request,HttpServletResponse response){
		 List<Book> bookLists = bookService.findByAllBook();
		 request.setAttribute("bookLists", bookLists);
		 return "change:/adminjsps/admin/book/list.jsp";
	}
	
	public String updateUI(HttpServletRequest request,HttpServletResponse response){
			String bid = request.getParameter("bid");
			Book book = bookService.findByIdBook(bid);
			List<Category> categoryLists = categoryService.findAllCategory();
			request.setAttribute("book", book);
			request.setAttribute("categoryLists", categoryLists);
			return "change:/adminjsps/admin/book/desc.jsp";
	}
	public String edit(HttpServletRequest request,HttpServletResponse response){
		// 需要在添加图书的页面上显示添加图书的类型
		List<Category> categoryLists = categoryService.findAllCategory();
		request.setAttribute("categoryLists", categoryLists);
		return "change:/adminjsps/admin/book/add.jsp";
	}
	public String repository(HttpServletRequest request,HttpServletResponse response){
		
		List<Book> bookLists=bookService.findByBookState();
		request.setAttribute("bookLists", bookLists);
		return "change:/adminjsps/admin/book/repository.jsp";
	}
	
	public String pushDown(HttpServletRequest request,HttpServletResponse response){
		String bid = request.getParameter("bid");
		Book book = bookService.findByIdBook(bid);
		// 修改状态信息 (0:上架 1:下架)
		book.setIsdel(1);
		bookService.update(book);
		return findAllBook(request,response);
	}
	public String pushUp(HttpServletRequest request,HttpServletResponse response){

		String bid = request.getParameter("bid");
		System.out.println(bid );
		Book book = bookService.findByIdBook(bid);
		System.out.println(book);
		book.setIsdel(0);
		bookService.update(book);
		return findAllBook(request,response);
	}
	
}
